package com.iot.manage.biz.controller;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.iot.common.core.exception.ModelException;
import com.iot.common.core.util.R;
import com.iot.common.core.util.UuidUtil;
import com.iot.manage.api.domain.SysLog;
import com.jfinal.kit.StrKit;

import javax.validation.Valid;

/**
 * 日志表 前端控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/log")
@Api(value = "log", tags = "日志管理模块")
public class LogController {


	@Setter
	private Throwable cause;
	
	
	/**
	 * 删除日志
	 * @param id ID
	 * @return success/false
	 */
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_log_del')")
	public R<String> deleteById(@PathVariable String id) {
		if(SysLog.dao.deleteById(id)) {
			return R.ok();
		}
		return R.failed();
	}

	/**
	 * 插入日志
	 * @param sysLog 日志实体
	 * @return success/false
	 */
	@PostMapping
	public R<String> save(@Valid @RequestBody SysLog sysLog) {
		if(sysLog==null) {
			throw new ModelException("日志对象不可以为空！");
		}
		if(StrKit.isBlank(sysLog.getId())) {
			sysLog.setId(UuidUtil.getUUID());
			if(sysLog.save()) {
				return R.ok();
			}
		}else {
			if(sysLog.update()) {
				return R.ok();
			}
		}
		return R.failed();
	}

}
